<?php
/**
 * 企业用户控制器
 * 
 * @author ThankPHP
 * 
 * @link Http://www.ThankPHP.Com
 * 
 * @copyright Copyright (c) 2009 http://thankphp.com All rights reserved.
 */
class EuserAction extends Action {
	/**
	 * 管理所有的企业用户分页显示
	 */
	public function AdminIndex()
	{
		EasyACL('Euser','AdminIndex',$_SESSION);
		$EUser = D('euser');
		$Count = $EUser->count('','euser_id');
		$listRows = 10;
		$fields = 'euser_id,username,q,maturity,from,lock';
		//引入分页
		import('ORG.Util.Page');
		$p = new Page($Count,$listRows);
		$list = $EUser->findall(null,$fields,'euser_id desc',$p->firstRow.','.$p->listRows);
		$page = $p->show();
		//模板输出
		$this->assign('EUsers',$list);
		$this->assign('page',$page);
		$this->display();
	}

	/**
	 * 创建新的企业用户
	 * */
	public function AdminCreateUser()
	{
		EasyACL('Euser','AdminCreateUser',$_SESSION);
		//如果是POST提交
		if ($this->isPost()) {
			// 验证2次输入密码是否一直
			$Pwd1 = $_POST['pwd1'];
			$Pwd2 = $_POST['pwd2'];
			if ($Pwd1 != $Pwd2) {
				halt('两次输入密码不一致!');
			}
			//获取程序运行时间
			$RunTime = time();
			$EUser = D('euser');
			$EUser->username = htmlspecialchars($_POST['username']);
			$EUser->password = md5($Pwd1);
			//如果管理员没有选择权限则自动注册成普通个人用户权限
			$EUser->q = (isset($_POST['q']))?htmlspecialchars($_POST['q']):'vip0';
			$EUser->name = htmlspecialchars($_POST['name']);
			$EUser->from = htmlspecialchars($_POST['from']);
			$EUser->tel = htmlspecialchars($_POST['tel']);
			$EUser->fax = htmlspecialchars($_POST['fax']);
			$EUser->mail = htmlspecialchars($_POST['mail']);
			$EUser->note = htmlspecialchars($_POST['note']);
			$EUser->maturity = strtotime($_POST['maturity']);
			$EUser->lock = htmlspecialchars($_POST['lock']);
			$EUser->created = $RunTime;
			$EUser->updated = $RunTime;
			$Re = $EUser->add();
			//判断是否注册成功
			if (!$Re) {
				halt('尝试注册账号[ '.htmlspecialchars($_POST['username']).' ]失败!');
			} else {
				$this->redirect('AdminIndex','Euser');
			}
		}
		$this->display();
	}

	/**
	 * 修改企业用户信息
	 */
	public function AdminUpdateUser()
	{
		EasyACL('Euser','AdminUpdateUser',$_SESSION);
		//POST提交
		if ($this->isPost()) {
			$euser_id = (isset($_POST['euser_id']))?(int)$_POST['euser_id']:halt('未获取到用户!');
			$pwd1 = $_POST['pwd1'];
			$pwd2 = $_POST['pwd2'];
			
			$User = D('euser');
			//设置查询条件
			$User->find($euser_id);
			$User->username = htmlspecialchars($_POST['username']);
			//如果两次输入密码不等于空 and 两次输入密码一致
			if ($pwd1 != '' and $pwd2 != '' and $pwd1 == $pwd2) {
				$User->password = md5($pwd1);
			} elseif ($pwd1 == '' and $pwd2 == '') {
				
			}
			$User->q = (isset($_POST['q']))?htmlspecialchars($_POST['q']):'vip0'; //权限
			$User->name = htmlspecialchars($_POST['name']); //名字
			$User->from = htmlspecialchars($_POST['from']); //来自
			$User->tel = htmlspecialchars($_POST['tel']); //电话
			$User->fax = htmlspecialchars($_POST['fax']); //传真
			$User->mail = htmlspecialchars($_POST['mail']); //邮箱
			$User->note = htmlspecialchars($_POST['note']); //其他说明
			$User->maturity = strtotime($_POST['maturity']); //到期时间
			$User->lock = htmlspecialchars($_POST['lock']); //锁定
			$User->updated = time();
			$Re = $User->Save();
			if (!$Re) {
				halt('尝试修改账号信息失败!');
			}
			$this->redirect('AdminIndex','EUser');
		}
		//获取要修改的用户信息
		$euser_id = (isset($_GET['euser_id']))?(int)$_GET['euser_id']:halt('请选择要修改的用户!');
		$User = D('euser');
		//设置查询条件
		$condition = array();
		$condition['euser_id'] = $euser_id;

		$UserInfo = $User->where($condition)->find();
		$this->assign('User',$UserInfo);
		$this->display();
	}

	/**
	 * 删除单个用户
	 */
	public function AdminDelUser()
	{
		EasyACL('Euser','AdminDelUser',$_SESSION);
		$euser_id = (isset($_GET['euser_id']))?(int)$_GET['euser_id']:halt('没有传入值!');
		$User = D('euser');
		$Re = $User->deleteById($euser_id,true);
		//判断
		if (!$Re) {
			halt('错误！尝试删除编号为' . $euser_id . '账号失败');
		}
		$this->redirect('AdminIndex','Euser');
	}

	/**
	 * 删除多个用户
	 */
	public function AdminDelUsers()
	{
		EasyACL('Euser','AdminDelUsers',$_SESSION);
		//判断是否为POST提交
		if ($this->isPost()) {
			$Uids = (isset($_POST['euser_id']))?$_POST['euser_id']:halt('传入值不正确!');
			$Error = '';
			$User = D('euser');
			foreach ($Uids as $uid) {
				$Re = $User->delete($uid);
				if (!$Re) {
					$Error .= '尝试删除编号为' . $uid . '失败!<br>';
				}
			}
			if (empty($Error)) {
				$this->redirect('AdminIndex','Euser');
			} else {
				halt($Error);
			}
		}
		//如果不是POST提交，则跳转到用户列表页面
		$this->redirect('AdminIndex','Euser');
	}

	/**
	 * 搜索用户
	 */
	public function AdminSearchUser(){
		//权限判断
		EasyACL('Euser','AdminSearchUser',$_SESSION);
		if ($this->isPost()) {
			$Username = htmlspecialchars($_REQUEST['username']);
			$User = D('euser');
			//设置查询条件
			import('ORG.Util.HashMap');
			$conditions = new HashMap();
			$conditions->put('username',array('like',"%$Username%"));
			
			//进行查询
			$Users = $User->where($conditions)->order('euser_id desc')->findall();
			
			//设置到模板
			$this->assign('Username',$Username);
			$this->assign('EUsers',$Users);
			$this->display();
		}
	}

	/**
	 * 锁定账号
	 */
	public function AdminUpdateLock(){
		EasyACL('Euser','AdminUpdateLock',$_SESSION);
		$uid = (int)$_GET['euser_id'];
		$lock = htmlspecialchars($_GET['lock']);
		//提交到数据库
		$User = D('euser');
		$User->find($uid);
		$User->lock = $lock;
		$Re = $User->save();
		if (!$Re) {
			halt('尝试修改状态失败!');
		}
		$this->redirect('AdminIndex','Euser');
	}

	/**
	 * 查看用户详细信息
	 */
	public function AdminDisplayUser(){
		EasyACL('Euser','AdminDisplayUser',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		
		//组成查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('euser_id',array('EQ',$Input->get('Euser_id')));
		//进行数据库查询
		$User = D('euser');
		$UserInfo = $User->where($conditions)->find();
		
		//设置到模板
		$this->assign('UserInfo',$UserInfo);
		$this->display();
	}
	
	/**
	 * 获取用户ID
	 */
	public function AdminGetUserId(){
		EasyACL('Euser','AdminGetUserId',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//构造查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('username',array('EQ',$Input->post('euser_id')));
		//进行数据库查询
		$User = D('euser');
		$UserInfo = $User->where($conditions)->find();
		echo json_encode($UserInfo);
	}
}